package salonika.demo;

import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.sl.usermodel.PlaceableShape;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableStyle;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by feng on 2023/1/5
 */
public class PPTDemo {


	public static void main(String[] s) throws IOException {
		test7();
	}

	private static void test1() throws IOException {
		//create a new empty slide show
		XMLSlideShow ppt = new XMLSlideShow();

		//add first slide
		XSLFSlide blankSlide = ppt.createSlide();
		FileOutputStream out = new FileOutputStream("C:\\tmp\\merged.pptx");
		ppt.write(out);
		out.close();
	}

	private static void test2() throws IOException {
		XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("C:\\tmp\\merged.pptx"));

		//append a new slide to the end
		XSLFSlide blankSlide = ppt.createSlide();
		FileOutputStream out = new FileOutputStream("C:\\tmp\\merged2.pptx");
		ppt.write(out);
		out.close();
	}

	private static void test3() throws IOException {
		XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("C:\\tmp\\merged.pptx"));

		// first see what slide layouts are available :
		System.out.println("Available slide layouts:");
		for(XSLFSlideMaster master : ppt.getSlideMasters()){
			for(XSLFSlideLayout layout : master.getSlideLayouts()){
				System.out.println(layout.getType());
			}
		}

		// blank slide
		XSLFSlide blankSlide = ppt.createSlide();

		// there can be multiple masters each referencing a number of layouts
		// for demonstration purposes we use the first (default) slide master
		XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);

		// title slide
		XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);
		// fill the placeholders
		XSLFSlide slide1 = ppt.createSlide(titleLayout);
		XSLFTextShape title1 = slide1.getPlaceholder(0);
		title1.setText("First Title");

		// title and content
		XSLFSlideLayout titleBodyLayout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
		XSLFSlide slide2 = ppt.createSlide(titleBodyLayout);

		XSLFTextShape title2 = slide2.getPlaceholder(0);
		title2.setText("Second Title");

		XSLFTextShape body2 = slide2.getPlaceholder(1);
		body2.clearText(); // unset any existing text
		body2.addNewTextParagraph().addNewTextRun().setText("First paragraph");
		body2.addNewTextParagraph().addNewTextRun().setText("Second paragraph");
		body2.addNewTextParagraph().addNewTextRun().setText("Third paragraph");
		FileOutputStream out = new FileOutputStream("C:\\tmp\\merged3.pptx");
		ppt.write(out);
		out.close();
	}

	private static void test4() throws IOException {
		XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("C:\\tmp\\merged.pptx"));
		// get slides
		for (XSLFSlide slide : ppt.getSlides()) {
			for (XSLFShape sh : slide.getShapes()) {
				// name of the shape
				String name = sh.getShapeName();

				// shapes's anchor which defines the position of this shape in the slide
				if (sh instanceof PlaceableShape) {
					java.awt.geom.Rectangle2D anchor = ((PlaceableShape)sh).getAnchor();
				}

				if (sh instanceof XSLFConnectorShape) {
					XSLFConnectorShape line = (XSLFConnectorShape) sh;
					// work with Line
				} else if (sh instanceof XSLFTextShape) {
					XSLFTextShape shape = (XSLFTextShape) sh;
					// work with a shape that can hold text
				} else if (sh instanceof XSLFPictureShape) {
					XSLFPictureShape shape = (XSLFPictureShape) sh;
					// work with Picture
				}
			}
		}
	}

	private static void test5() throws IOException {
		XMLSlideShow ppt = new XMLSlideShow();
		XSLFSlide slide = ppt.createSlide();

		byte[] pictureData = IOUtils.toByteArray(new FileInputStream("C:\\tmp\\image.jpg"));

		XSLFPictureData pd = ppt.addPicture(pictureData, PictureData.PictureType.JPEG);
		XSLFPictureShape pic = slide.createPicture(pd);
		Rectangle2D rectangle = new Rectangle(200, 200,
				100, 100);
		pic.setAnchor(rectangle);
		FileOutputStream out = new FileOutputStream("C:\\tmp\\merged5.pptx");
		ppt.write(out);
		out.close();
	}

	private static void test6() throws IOException {
		XMLSlideShow ppt = new XMLSlideShow();
		XSLFSlide slide = ppt.createSlide();

		XSLFTable table = slide.createTable(5,3);

		Rectangle2D rectangle = new Rectangle(200, 200,
				300, 300);
		table.setAnchor(rectangle);
		for( XSLFTableRow row : table.getRows()){
			for(XSLFTableCell cell : row.getCells()){
				/** 设置单元格的边框颜色 **/
				cell.setBorderColor(TableCell.BorderEdge.bottom, new Color(10,100,120));
				cell.setBorderColor(TableCell.BorderEdge.top, new Color(10,100,120));
				cell.setBorderColor(TableCell.BorderEdge.left, new Color(10,100,120));
				cell.setBorderColor(TableCell.BorderEdge.right, new Color(10,100,120));

				/** 设置单元格边框 **/
				cell.setBorderWidth(TableCell.BorderEdge.bottom, 3);
				cell.setBorderWidth(TableCell.BorderEdge.top, 3);
				cell.setBorderWidth(TableCell.BorderEdge.left, 3);
				cell.setBorderWidth(TableCell.BorderEdge.right, 3);
				/** 设置文本 **/


				cell.clearText();
//				cell.addNewTextParagraph().addNewTextRun().setFontSize(10.0);
				cell.setText("AAA").setFontSize(10.0);
			}
		}
		table.getCell(1,1).setText("测试");
		FileOutputStream out = new FileOutputStream("C:\\tmp\\merged6.pptx");
		ppt.write(out);
		out.close();
	}

	private static void test7() throws IOException {
		XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("C:\\tmp\\merged6.pptx"));

		XSLFSlide slide = ppt.createSlide();
		slide.getSlideMaster().addShape(slide.getShapes().get(4));

	}

}
